//
//  LSZNewPrayToWitnessVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/12/26.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZNewPrayToWitnessVC.h"
 

@interface LSZNewPrayToWitnessVC ()
@property(nonatomic,strong) UIView * viewTop;
@property(nonatomic,strong) UIView * viewRed;
@property(nonatomic,strong) UIScrollView * scrollViewBack;

@property(nonatomic,strong) UITableView * tableViewPrayer;
@property(nonatomic,strong) UITableView * tableViewWitness;



@property(nonatomic,strong) NSMutableArray * arrayData;
@property(nonatomic,assign) NSInteger numberPage;

@property(nonatomic,strong) NSMutableArray * arrayWitnessData;
@property(nonatomic,assign) NSInteger pageWitness;
@end

@implementation LSZNewPrayToWitnessVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.numberPage = 1;
    self.pageWitness = 1;
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [self addMutDataDic:self.numberPage];
    [self addMutWitnessDataDic:self.pageWitness];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self emptyDataRemoveAllSubviews];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)addHuanCUnDataDic :(NSInteger)numberPage :(NSString *)cid
{
    NSString * strid = [NSString stringWithFormat:@"%@_1_%@",shengjing_daogaojianzheng,cid];
    [LSZCacheHandling seeIfThereIsCache:@"lsz_mygd" :strid :^(BOOL isoK, NSMutableArray *array) {
        if (isoK == YES) {
            LSZPrayerAndTestimonyManager * manager = [[LSZPrayerAndTestimonyManager alloc]initWithPrayerAndTestimonyArray:array];
            if([cid isEqualToString:@"0"]){
                [self.arrayData removeAllObjects];
                [self.arrayData addObjectsFromArray:manager.mutMgArray];
                if (self.arrayData.count == 0) {
                    LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                    [vc showWithView:self.tableViewPrayer];
                }else{
                    [self emptyDataRemoveAllSubviews];
                }
                [self.tableViewPrayer reloadData];
            }else{
                [self.arrayWitnessData removeAllObjects];
                [self.arrayWitnessData addObjectsFromArray:manager.mutMgArray];
                if (self.arrayWitnessData.count == 0) {
                    LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                    [vc showWithView:self.tableViewWitness];
                }else{
                    [self emptyDataRemoveAllSubviews];
                }
                [self.tableViewWitness reloadData];
            }
        }else{
            [self setStringWarning:NoNetwork];
        }
    }];
}
#pragma mark - 获取祷告数据
-(void)addMutDataDic :(NSInteger)numberPage
{
    [LSZRequestResolutionDic initWithAllPrayersAndTestimonies:@"0" page:[NSString stringWithFormat:@"%zd",(long)numberPage] quBie:1 :^(BOOL isOK, NSString * Str, LSZPrayerAndTestimonyManager * manager) {
        if (isOK == YES) {
            if (numberPage == 1) {
                [self.arrayData removeAllObjects];
                [self.arrayData addObjectsFromArray:manager.mutMgArray];
                if(manager.mutMgArray.count == 0 ){
                    self.tableViewPrayer.mj_footer.hidden = YES;
                    LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                    [vc showWithView:self.tableViewPrayer];
                }else{
                    [self emptyDataRemoveAllSubviews];
                }
            }else{
                [self.arrayData addObjectsFromArray:manager.mutMgArray];
            }
            if(manager.mutMgArray.count == 0){
                [self.tableViewPrayer.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableViewPrayer reloadData];

        }else{
            self.numberPage = 1;
            [self addHuanCUnDataDic:1 :@"0"];
        }
    }];
}
#pragma mark - 获取见证数据
-(void)addMutWitnessDataDic :(NSInteger)numberPage
{
    [LSZRequestResolutionDic initWithAllPrayersAndTestimonies:@"1" page:[NSString stringWithFormat:@"%zd",(long)numberPage] quBie:1 :^(BOOL isOK, NSString * Str, LSZPrayerAndTestimonyManager * manager) {
        if (isOK == YES) {
            if (numberPage == 1) {
                [self.arrayWitnessData removeAllObjects];
                [self.arrayWitnessData addObjectsFromArray:manager.mutMgArray];
                if(manager.mutMgArray.count == 0 ){
                    self.tableViewWitness.mj_footer.hidden = YES;
                    LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                    [vc showWithView:self.tableViewWitness];
                }else{
                    [self emptyDataRemoveAllSubviews];
                }
            }else{
                [self.arrayWitnessData addObjectsFromArray:manager.mutMgArray];
            }
            if(manager.mutMgArray.count == 0){
                [self.tableViewWitness.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableViewWitness reloadData];
        }else{
            self.pageWitness = 1;
            [self addHuanCUnDataDic:1 :@"1"];
        }
    }];
}
#pragma mark - 移除空的数据
-(void)emptyDataRemoveAllSubviews
{
    NSNotification *notificationCollection =[NSNotification notificationWithName:@"RemoveAllSubviews" object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notificationCollection];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RemoveAllSubviews" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的祷告见证";
    self.arrayData = [[NSMutableArray alloc]init];
    self.arrayWitnessData = [[NSMutableArray alloc]init];

    [self setScrollViewUI];
    [self.tableViewPrayer registerClass:[LSZDGJZTableViewCell class] forCellReuseIdentifier:@"LSZDGJZTableViewCell"];
    [self.tableViewWitness registerClass:[LSZDGJZTableViewCell class] forCellReuseIdentifier:@"LSZDGJZTableViewCell"];

    [self.tableViewPrayer registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCelldgjz"];
    [self.tableViewWitness registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCelldgjz"];

    self.tableViewPrayer.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.numberPage = 1;
        //诗歌歌谱的分类
        [self addMutDataDic :self.numberPage];
        [self.tableViewPrayer.mj_header endRefreshing];
    }];
    self.tableViewPrayer.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.numberPage++;
        //诗歌歌谱的分类
        [self addMutDataDic:self.numberPage];
        [self.tableViewPrayer.mj_footer endRefreshing];
    }];

    self.tableViewWitness.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageWitness = 1;
        //诗歌歌谱的分类
        [self addMutWitnessDataDic:self.pageWitness];
        [self.tableViewWitness.mj_header endRefreshing];
    }];
    self.tableViewWitness.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageWitness++;
        //诗歌歌谱的分类
        [self addMutWitnessDataDic:self.pageWitness];
        [self.tableViewWitness.mj_footer endRefreshing];
    }];
    //完成按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 40, 40);
    [rightBtn setTitle:@"发布" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//先暂时改颜色
    rightBtn.titleLabel.font = FONT_16;
    [rightBtn addTarget:self action:@selector(rightBtnPosted) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    /**增加侧滑返回,无其他作用*/
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, ScreenHeight)];
    [self.view addSubview:view];
    [self.view bringSubviewToFront:view];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSZPrayerAndTestimonyMg * model = nil;
    if (tableView == self.tableViewPrayer) {
        if(self.arrayData.count >indexPath.row){
            model = self.arrayData[indexPath.row];
        }
    }else{
        if(self.arrayWitnessData.count > indexPath.row){
            model = self.arrayWitnessData[indexPath.row];
        }
    }
    if ([model.type isEqualToString:@"-1"]) {
        NSInteger number = indexPath.row/5;
        if (self.adViewArray.count > number){
            return  360;
        }else{
            return 0.000001;
        }
    }else{
        if (ScreenWidth == 320) {
            //5
            CGFloat cellH = [self initWithStrHeigh:model.content];
            return (cellH+109+(ScreenWidth-30)/16*9);
        }else if(ScreenWidth == 375){
            // 6 7
            CGFloat cellH = [self initWithStrHeigh:model.content];
            return (cellH+114+(ScreenWidth-30)/16*9);
        }else {
            //plus
            CGFloat cellH = [self initWithStrHeigh:model.content];
            return (cellH+119+(ScreenWidth-30)/16*9);
        }
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableViewPrayer) {
        if(self.arrayData.count > 0){
            return  self.arrayData.count;
        }
        return  0;
    }else{
        if(self.arrayWitnessData.count > 0){
            return self.arrayWitnessData.count;
        }
        return 0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZPrayerAndTestimonyMg  * manager = nil;
    if (tableView == self.tableViewPrayer) {
        if(self.arrayData.count > indexPath.row){
            manager = self.arrayData[indexPath.row];
        }
    }else{
        if (self.arrayWitnessData.count >indexPath.row) {
            manager = self.arrayWitnessData[indexPath.row];
        }
    }
    LSZDGJZTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZDGJZTableViewCell" forIndexPath:indexPath];
    cell.manager = manager;
    CGRect rect = cell.labelTitle.frame;
    rect.origin.x = cell.labelSelect.frame.origin.x;
    cell.labelTitle.frame = rect;
    cell.labelSelect.hidden = YES;
    cell.butFocusOn.hidden = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZPrayerAndTestimonyMg  * manager = nil;
    NSString * cid = [[NSString alloc]init];
    if (tableView == self.tableViewPrayer) {
        cid = @"0";
        if(self.arrayData.count > indexPath.row){
            manager = self.arrayData[indexPath.row];
        }
    }else{
        cid = @"1";
        if (self.arrayWitnessData.count > indexPath.row) {
            manager = self.arrayWitnessData[indexPath.row];
        }
    }
    if ([manager.type isEqualToString:@"-1"]) {
        return;
    }
    [LSZSpecialPackaging addPrayerNumber:manager.id_id :^(BOOL isok) {
        if(isok == YES){
            NSString * strid = [NSString stringWithFormat:@"%@_1_%@",shengjing_daogaojianzheng,cid];
            NSInteger num = [manager.browseNum  integerValue]+1;
            manager.browseNum = [NSString  stringWithFormat:@"%zd",num];
            [LSZCacheHandling modifyingParameterOfData:@"lsz_mygd" SmallDirectory:strid key:@"id" value:manager.id_id parameter:@"browseNum" modifiedValue:[NSString stringWithFormat:@"%zd",num]];
        }
    }];
    LSZPrayerContentEvaluationVC * vc = [[LSZPrayerContentEvaluationVC  alloc]init];
    vc.resource_id = manager.id_id;
    if([manager.type isEqualToString:@"0"]){
        vc.strTitle = [NSString stringWithFormat:@"%@为%@关于%@的祷告",manager.publisher,manager.toName,manager.typeName];
    }else{
        vc.strTitle = [NSString stringWithFormat:@"%@为%@关于%@的见证",manager.publisher,manager.title,manager.typeName];
    }
    vc.imageViewHeader = manager.publisher_pic;
    vc.imageUrl = manager.imageUrl;
    vc.strName  = manager.publisher;
    vc.strTime = manager.createTime;
    vc.strtype = manager.type;
    vc.strtypeName = manager.typeName;
    vc.strContent = manager.content;
    vc.id_id = manager.id_id;
    vc.lastNumber = (NSInteger)indexPath.row;
    vc.arrayData = self.arrayData;
    vc.comeFrame = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 发布按钮
-(void)rightBtnPosted
{
    if([[_UserM usercode] isEqualToString:@""]){
        [self gotoLogin];
    }else{
        LSZReleasePrayerAndWitnessVC * vc = [[LSZReleasePrayerAndWitnessVC alloc]init];
        vc.choosePosted = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 祷告
-(void)clickPrayer
{
    [self.scrollViewBack setContentOffset:CGPointMake(0, 0) animated:YES];
    [self MovePrayer];
}
-(void)MovePrayer
{
    [LCFloatingWindow sharedInstance].hidden = YES;
    self.viewRed.frame = CGRectMake(self.viewTop.frame.size.width/6,self.viewTop.frame.size.height-1,self.viewTop.frame.size.width/6, 1);
    if (self.arrayData.count == 0) {
        LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
        [vc showWithView:self.tableViewPrayer];
    }else{
        [self emptyDataRemoveAllSubviews];
    }
}
#pragma mark - 见证
-(void)clickWitness
{
    [self.scrollViewBack setContentOffset:CGPointMake(ScreenWidth, 0) animated:YES];
    [self MoveWitness];
}
-(void)MoveWitness
{
    [LCFloatingWindow sharedInstance].hidden = YES;
    self.viewRed.frame = CGRectMake(self.viewTop.frame.size.width/6*4,self.viewTop.frame.size.height-1,self.viewTop.frame.size.width/6, 1);
    if (self.arrayWitnessData.count == 0) {
        LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
        [vc showWithView:self.tableViewWitness];
    }else{
        [self emptyDataRemoveAllSubviews];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.tableViewPrayer||scrollView == self.tableViewWitness) {
        return ;
    }
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    if (index == 0) {
        [self MovePrayer];
    }
    else{
        [self MoveWitness];
    }
}
#pragma mark - 滚动视图
-(void)setScrollViewUI
{
    self.viewTop = [[UIView alloc]initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth,50)];
    self.viewTop.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewTop];

    UIButton * butAlbum = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2,self.viewTop.frame.size.height)];
    [butAlbum setTitle:@"祷告" forState:UIControlStateNormal];
    [butAlbum setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [butAlbum addTarget:self action:@selector(clickPrayer) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butAlbum];

    UIButton * butSinger = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2, 0, ScreenWidth/2,self.viewTop.frame.size.height)];
    [butSinger setTitle:@"见证" forState:UIControlStateNormal];
    [butSinger setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [butSinger addTarget:self action:@selector(clickWitness) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butSinger];

    UIView * viewWhite = [[UIView alloc]initWithFrame:CGRectMake(0,self.viewTop.frame.size.height-0.5, ScreenWidth, 0.5)];
    viewWhite.backgroundColor = divider;
    [self.viewTop addSubview:viewWhite];

    UIView * viewVerticalBar = [[UIView alloc]initWithFrame:CGRectMake((self.viewTop.frame.size.width-0.5)/2, 10 , 0.5, self.viewTop.frame.size.height-20)];
    viewVerticalBar.backgroundColor = divider;
    [self.viewTop addSubview:viewVerticalBar];

    self.viewRed = [[UIView alloc]initWithFrame:CGRectMake(self.viewTop.frame.size.width/6, self.viewTop.frame.size.height-1,self.viewTop.frame.size.width/6, 1)];
    self.viewRed.backgroundColor = redUIColor;
    [self.viewTop addSubview:self.viewRed];

    self.scrollViewBack = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewTop.frame), ScreenWidth, ScreenHeight-CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT)];
    self.scrollViewBack.delegate = self;
    self.scrollViewBack.bounces = NO; // 默认为YES 取消设置NO
    // 当bounces属性为NO时，以下两个属性设置无效
    self.scrollViewBack.alwaysBounceHorizontal = YES; // 水平方向弹簧效果
    self.scrollViewBack.alwaysBounceVertical = YES; // 垂直方向弹簧效果
    self.scrollViewBack.decelerationRate = 0.1; // 值域 0~1 值越小减速停止的时间越短
    self.scrollViewBack.pagingEnabled = YES; // 默认为NO，设置YES内容视图整页滚动
    self.scrollViewBack.showsVerticalScrollIndicator = YES; // 垂直方向
    self.scrollViewBack.contentSize = CGSizeMake(ScreenWidth*2, ScreenHeight-CGRectGetMaxY(self.viewTop.frame));
    [self.view addSubview:self.scrollViewBack];


    self.tableViewPrayer = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
    self.tableViewPrayer.delegate = self;
    self.tableViewPrayer.dataSource = self;
    self.tableViewPrayer.estimatedRowHeight = 0;
    self.tableViewPrayer.estimatedSectionFooterHeight =0;
    self.tableViewPrayer.estimatedSectionHeaderHeight =0;
    self.tableViewPrayer.tableFooterView = [UIView new];
    [self.scrollViewBack addSubview:self.tableViewPrayer];

    self.tableViewWitness = [[UITableView alloc]initWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight-CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
    self.tableViewWitness.delegate = self;
    self.tableViewWitness.dataSource = self;
    self.tableViewWitness.estimatedRowHeight = 0;
    self.tableViewWitness.estimatedSectionFooterHeight =0;
    self.tableViewWitness.estimatedSectionHeaderHeight =0;
    self.tableViewWitness.tableFooterView = [UIView new];
    [self.scrollViewBack addSubview:self.tableViewWitness];
}
-(CGFloat)initWithStrHeigh :(NSString * )str
{
    CGFloat fontType;
    if (ScreenWidth == 320) {
        //5
        fontType = 13.5;
    }else if(ScreenWidth == 375){
        // 6 7
        fontType = 14;
    }else {
        //plus
        fontType = 14.5;
    }
    CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth-20, ScreenHeight*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_16 forKey:NSFontAttributeName] context:nil];
    return labTitleRect.size.height+15;
}
@end
